package com.health.task;

import com.health.constant.RedisConstant;
import com.health.util.AliyunUploadUtils;
import com.health.util.QiNiuUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Set;

/**
 * @Description
 * @Author konglz
 * @Data 2023/3/29 14:39
 */
@Configuration
@Slf4j
public class DeleteQiniuImageTask {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Scheduled(cron = "0 0/1 * * * ? ")
    public void clearImage(){
        //1、得到redis的两个集合得的差集
        Set<String> difference = stringRedisTemplate.opsForSet().difference(
                RedisConstant.REDIS_ALL_UPLOAD_IMAGE,
                RedisConstant.REDIS_DB_UPLOAD_IMAGE
        );
        //2、删除阿里云无效图片
        if(difference != null && difference.size() > 0){
            for (String s : difference) {
                //3、删除redis中无效的图片
                AliyunUploadUtils.deleteImg(s);
                stringRedisTemplate.opsForSet().remove(RedisConstant.REDIS_ALL_UPLOAD_IMAGE, s);
            }
        }
    }

    @Scheduled(cron = "0 0/3 * * * ?")
    public void clearImageSet(){
        log.info("=====清除redis图片集合定时任务开始=====");
        // 清空之前先执行一次清除无效图片的方法
        clearImage();
        // 清空两个集合
        stringRedisTemplate.delete(RedisConstant.REDIS_ALL_UPLOAD_IMAGE);
        stringRedisTemplate.delete(RedisConstant.REDIS_DB_UPLOAD_IMAGE);
    }
}
